{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using filters in PerspectiveWidget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from datetime import date, datetime\n",
    "from perspective import PerspectiveWidget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.DataFrame({\n",
    "    \"set\": [True, False, True, False],\n",
    "    \"num\": np.arange(4)\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "''' PerspectiveWidget supports a wide range of filter conditions inside the `filters` kwarg.\n",
    "\n",
    "Pass `filters` a list of lists - each element is a list of three values:\n",
    "    - [0]: a string column name to filter on\n",
    "    - [1]: a string filter operation, i.e. \"<\", \">\", \"==\", \"is null\", \"is not null\"\n",
    "    - [2]: a value to filter on (not needed if using \"is null\"/\"is not null\" filters)\n",
    "'''\n",
    "widget = PerspectiveWidget(data, filters=[[\"set\", \"==\", True]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data2 = pd.DataFrame({\n",
    "    \"time\": [datetime(2019, 6, 10, 12, 30), datetime(2019, 6, 10, 14, 30), datetime(2019, 6, 11, 12, 30), datetime(2019, 6, 11, 14, 30)],\n",
    "    \"date\": [date(2019, 6, 10), date(2019, 6, 11), date(2019, 6, 12), date(2019, 6, 13)],\n",
    "    \"num\": np.arange(4),\n",
    "    \"nullable\": [None, 1, None, 2]\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# filtering on \"is null\" or \"not null\" does not require a comparison value\n",
    "widget2 = PerspectiveWidget(data2, filters=[[\"nullable\", \"is null\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "widget2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "widget2.filters = [[\"index\", \"<\", 1]] # apply a new set of filters to the widget"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
